package com.tedu.egmvc.controller;

import com.tedu.egmvc.mapper.AddressMapper;
import com.tedu.egmvc.pojo.dto.AddressDTO;
import com.tedu.egmvc.pojo.dto.AddressUpdateDTO;
import com.tedu.egmvc.pojo.entity.Address;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
public class AddressController {

    @Autowired
    private AddressMapper addressMapper;
    @RequestMapping("/address/insert")
    @ResponseBody
    public String insert(AddressDTO addressDTO){
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO, address);
        int num = addressMapper.insert(address);
        return num>0?"成功":"失败";
    }
    @RequestMapping("/address/update")
    @ResponseBody
    public String update(AddressUpdateDTO addressUpdateDTO){
        Address address = new Address();
        BeanUtils.copyProperties(addressUpdateDTO,address);
        int num = addressMapper.update(address);
        return num>0?"成功":"失败";
    }
    @RequestMapping("/address/delete")
    @ResponseBody
    public String delete(Integer id){
        int num = addressMapper.deleteById(id);
        return num>0?"成功":"失败";
    }
    @RequestMapping("/address/select")
    @ResponseBody
    public List<Address> select(){
        return addressMapper.selectAll();
    }
}
